package xby.study.proxy.cglib.invocationhandler;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
import xby.study.proxy.cglib.PathUtil;

import java.lang.reflect.Method;

public class InvocationHandlerTest {
    public void test() {
        System.out.println(this.getClass().getName());
    }


    public static void main(String[] args) {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, PathUtil.getOutPath(InvocationHandlerTest.class));
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(InvocationHandlerTest.class);
        /*
        * 单一  InvocationHandler callback 不会出发fastClass机制
        * */
        enhancer.setCallback(new InvocationHandler() {
            @Override
            public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
                //System.out.println(obj);  不能保留该行，否者会触发 代理对象toString 方法被无限调用！！！！！
                System.out.println("before method ..." + method);
                Object result = method.invoke(obj.getClass().getSuperclass().newInstance(), args);
                System.out.println("after method ..." + method
                );
                return result;
            }
        });
        InvocationHandlerTest test = (InvocationHandlerTest) enhancer.create();
        test.test();
    }
}
